<?php 
class login extends CI_Controller { 
	private $data;
    function login()
    {
		global $data;
     parent::__construct();
     $this->load->model('login_model');
	   
	   $this->load->model('common_model');
 
	$result=$this->common_model->getKeywords('login'); 
	$data['keywords']=$result;
    session_start();

   }
		
		
	

	public function index()
	  {
		global $data;
		//check aleready login ?
		if (isset($_SESSION['logged_in']))
			 {
		   redirect('loginsuccess');
		      }

		else{
	    $this->load->view('login',$data);
		}
	  }
    public function checklogin()
	  {
	global $data;
		 $result=$this->common_model->getKeywords('home'); 
		$data['keywords']=$result;
		

       $uname=$_POST['name'];
       $password=$_POST['password'];
	   $login_details=$this->login_model->login($uname,$password) ;

	   if($login_details!=false)
         {
        foreach ($login_details->result() as $row)
            {

            $_SESSION['logged_in']=$row->id;
            $_SESSION['name']=$row->name;
			$_SESSION['rollId']=$row->rollId;

         redirect('loginsuccess');
						
			}}
      else{
         
	     $this->session->set_flashdata('logged_error', TRUE);
               redirect('login');
	   } 
        
	   
		 
	  }
     function forgotpassword()
	  {
		global $data;
       $this->load->view('forgotpassword',$data);
	   }

	    function forgotpasslink()
	    {
			//todo md5

		$randomstring=rand();
	    $emailId=$_POST['emailtext'];
        $login_details=$this->login_model->forgotpassword($emailId,$randomstring) ;
       $result= $this->sendconfirmationlink($emailId,$randomstring);
      $this->session->set_flashdata('passwordstatus', $result);
		
  redirect('login/forgotpassword');
	     }

   function sendconfirmationlink($emailId,$randomstring)
	{ 
global $data;
$subject = "HTML email";
 
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <info@javatutions.com>' . "\r\n";

$currentLocation=MAIN_URL; 
	  
  $activationLink=$currentLocation."index.php/login/resetpassword?email=".$emailId."&randomlink=".$randomstring; mail($emailId,"Forgot Password Link",$activationLink,$headers); 


/*
	 	 $currentLocation=MAIN_URL; 
	  
   $activationLink=$currentLocation."/index.php/login/resetpassword?email=".$emailId."&randomlink=".$randomstring;  
  
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.googlemail.com';
        $config['smtp_port'] = '465';
        $config['smtp_user'] ='abcdemo001@gmail.com';
        $config['smtp_pass'] = 'demoaccount';
$this->load->library('email',$config);

$this->email->set_newline("\r\n");
$this->email->from("mohitmca2011@gmail.com");
$this->email->to("mohitmca2011@gmail.com");
$this->email->subject("123");
$this->email->message($activationLink);

if($this->email->send())
		{
	echo "Your email send";
		}

else {
show_error($this->email->print_debugger());
	}
 
*/
	}

function resetpassword()
	{
	global $data;
     $data['email']=$_GET['email'];
	 $data['randomlink']=$_GET['randomlink'];

    $this->load->view('resetfpasswordscreen',$data);

}


function updatepassword()
	{
	global $data;
	 $email=$_POST['email'];
	 $randomlink=$_POST['randomlink'];
	 $newpass=$_POST['newpass'];

    $login_details=$this->login_model->updateforgotpassword($email,$randomlink,$newpass) ;
$this->session->set_flashdata('passwordsuccess', true);
		
redirect('login/resetpasswordsuccess');
	     }
	 function resetpasswordsuccess()
	{
   
   

   if($this->session->flashdata('passwordsuccess'))
		{
	   $this->load->view('resetpasswordsuccess',$data);
		}

		else
		{
redirect('login');
		}
	}

function savepopupemail()
	{
	$email=$_POST['email'];
	 
    echo $this->login_model->savepopupemail($email);
	}

}